<script setup>
import { getPayOrderAPI, gotoPayOrder } from "@/apis/pay";
import { onMounted, ref } from "vue";
import { useRoute } from "vue-router";
import { useCountDown } from "@/composables/useCountDown";
import { reactive } from "vue";
import { calculate_signature } from "@/utils/common";

const { formatTime, start } = useCountDown();
const route = useRoute();
const payInfo = ref({});

let formParams = reactive({
  traceNo: route.query.orderId, // traceNo 为系统订单ID
  type: "ALIPAY", //  ALIPAY、ONEPAY 二选一
  totalAmount: "", // 付款金额
  subject: route.query.orderId, // 订单标题
  redirectUrl: "http://10.161.152.86/#/paycallback", // 支付完成回调接口
});

const getPayOrder = async () => {
  let res = await getPayOrderAPI(route.query.orderId);
  console.log("res: ", res);
  payInfo.value = res.data.orders;
  console.log("payInfo.value: ", payInfo.value);
  formParams = {
    traceNo: route.query.orderId, // traceNo 为系统订单ID
    type: "ALIPAY", //  ALIPAY、ONEPAY 二选一
    totalAmount: res.data.orders.totalAmount, // 付款金额
    subject: route.query.orderId, // 订单标题
    redirectUrl: "http://10.161.152.86/#/paycallback", // 支付完成回调接口
  };
  console.log("formParams ", formParams);
  start(res.data.orders.paymentDeadline);
};

onMounted(() => {
  getPayOrder();
});

const baseURL = "http://pcapi-xiaotuxian-front-devtest.itheima.net/";
const backURL = "http://10.161.152.86/paycallback";
const redirectUrl = encodeURIComponent(backURL);
const payUrl = `${baseURL}pay/aliPay?orderId=${route.query.id}&redirect=${redirectUrl}`;
const total = payInfo.value.totalAmount;
console.log("total: ", total);

const payRes = ref(null);

const submit = async (type) => {
  formParams.type = type;
  console.log("formParams: ", formParams, payInfo.value.totalAmount);
  const signature = calculate_signature({
    ...formParams,
  });
  const data = { ...formParams, signature };

  fetch("http://8.137.61.26:8080/payment/pay", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify(data),
  })
    .then((response) => {
      if (response.redirected) {
        window.location.href = response.url;
        return;
      }
      return response.text();
    })
    .then((html) => {
      if (html) document.write(html);
    });

  // gotoPayOrder(data).then((response) => {
  //   console.log("response: ", response);
  //   if (response.redirected) {
  //     window.location.href = response.url;
  //     return;
  //   }
  //   payRes.value = response;
  //   const div = document.createElement("div");
  //   div.innerHTML = response;
  //   document.body.appendChild(div);
  //   // return response.text();
  // });
  // .then((html) => {
  //   if (html) document.write(html);
  // });
};
</script>

<template>
  <div class="xtx-pay-page" v-if="!payRes">
    <div class="container">
      <!-- 付款信息 -->
      <div class="pay-info">
        <span class="icon iconfont icon-queren2"></span>
        <div class="tip">
          <p>订单提交成功！请尽快完成支付。</p>
          <p>
            支付还剩 <span> {{ formatTime }}</span
            >, 超时后将取消订单
          </p>
        </div>
        <div class="amount">
          <span>应付总额：</span>
          <span>¥{{ payInfo.totalAmount?.toFixed(2) }}</span>
        </div>
      </div>
      <!-- 付款方式 -->
      <div class="pay-type">
        <p class="head">选择以下支付方式付款</p>
        <div class="item">
          <p>支付平台</p>
          <!-- <a class="btn wx" href="javascript:;"></a> -->
          <!-- <a class="btn alipay" :href="payUrl"></a> -->
          <a class="btn alipay" @click="submit('ALIPAY')"></a>
          <a class="btn onepay" @click="submit('ONEPAY')"></a>
        </div>
        <!-- <div class="item">
          <p>支付方式</p>
          <a class="btn" href="javascript:;">招商银行</a>
          <a class="btn" href="javascript:;">工商银行</a>
          <a class="btn" href="javascript:;">建设银行</a>
          <a class="btn" href="javascript:;">农业银行</a>
          <a class="btn" href="javascript:;">交通银行</a>
        </div> -->
      </div>
    </div>
  </div>
  <div v-else class="xtx-pay-page"></div>
</template>

<style scoped lang="scss">
form {
  width: 500px;
  height: 500px;
  z-index: 999;
  background-color: #222;
}
.xtx-pay-page {
  margin-top: 20px;
}

.pay-info {
  background: #fff;
  display: flex;
  align-items: center;
  height: 240px;
  padding: 0 80px;

  .icon {
    font-size: 80px;
    color: #1dc779;
  }

  .tip {
    padding-left: 10px;
    flex: 1;

    p {
      &:first-child {
        font-size: 20px;
        margin-bottom: 5px;
      }

      &:last-child {
        color: #999;
        font-size: 16px;
      }
    }
  }

  .amount {
    span {
      &:first-child {
        font-size: 16px;
        color: #999;
      }

      &:last-child {
        color: $priceColor;
        font-size: 20px;
      }
    }
  }
}

.pay-type {
  margin-top: 20px;
  background-color: #fff;
  padding-bottom: 70px;

  p {
    line-height: 70px;
    height: 70px;
    padding-left: 30px;
    font-size: 16px;

    &.head {
      border-bottom: 1px solid #f5f5f5;
    }
  }

  .btn {
    width: 150px;
    height: 50px;
    border: 1px solid #e4e4e4;
    text-align: center;
    line-height: 48px;
    margin-left: 30px;
    color: #666666;
    display: inline-block;

    &.active,
    &:hover {
      border-color: $xtxColor;
    }

    &.alipay {
      background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/7b6b02396368c9314528c0bbd85a2e06.png)
        no-repeat center / contain;
    }

    &.onepay {
      background: url(http://8.137.61.26:9000/chat/2025-05-06/98c16b1a.png)
        no-repeat center / contain;
    }

    &.wx {
      background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/c66f98cff8649bd5ba722c2e8067c6ca.jpg)
        no-repeat center / contain;
    }
  }
}
</style>
